/*
 * Copyright (c) 2022, IMMORTA Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * - Neither the name of IMMORTA Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef WDG_DRV_H
#define WDG_DRV_H

/*!
 * @file wdg_driver.h
 * @brief This file declares WDG driver interfaces
 */

/*!
 * @addtogroup wdg_drv
 * @{
 */

/*******Includes***************************************************************/
#include <stdint.h>
#include <stdbool.h>
#include "stdio.h"
#include "errcodes.h"
#include "device_registers.h"

/*******Definitions************************************************************/

/*!
 * @brief WDG clock source
 */
typedef enum {
#if (CONFIG_WDG_SUPPORT_FUNC_CLK_HSI == 0U)
    WDG_CLK_SRC_APB = 0U,            /*!< WDG clock source is APB */
#elif (CONFIG_WDG_SUPPORT_FUNC_CLK_HSI == 1U)
    WDG_CLK_SRC_HSI = 0U,            /*!< WDG clock source is HSI */
#endif
    WDG_CLK_SRC_LSI = 1U             /*!< WDG clock source is LSI */
} wdg_clk_src_t;

/*!
 * @brief WDG config
 */
typedef struct {
    wdg_clk_src_t       clockSource;       /*!< WDG clock source */
    bool                updateEnable;      /*!< WDG update enable */
    bool                intEnable;         /*!< WDG interrupt enable */
    bool                windowModeEnable;  /*!< WDG window mode enable */
    bool                prescalerEnable;   /*!< WDG prescaler enable */
    uint16_t            windowValue;       /*!< WDG window value */
    uint16_t            timeoutValue;      /*!< WDG total time out value */
} wdg_config_t;


/*******APIs*******************************************************************/
#if defined(__cplusplus)
extern "C" {
#endif

/*!
 * @brief WDG init
 *
 * @param[in] instance: WDG instance number
 * @param[in] wdgConfig: WDG initial config
 * @return ERR_SUCCESS:Success
 *         ERR_ERROR:Failed, invalide param or not enable update
 */
errcode_t WDG_Init(uint32_t instance, const wdg_config_t* wdgConfig);

/*!
 * @brief WDG deinit
 *
 * @param[in] instance: WDG instance number
 * @return ERR_SUCCESS:Success
 *         ERR_ERROR:Failed, invalide param or not enable update
 */
errcode_t WDG_Deinit(uint32_t instance);

/*!
 * @brief Get default WDG config
 *
 * @param[out] wdgConfig: WDG default config
 * @return None
 */
void WDG_GetDefaultConfig(wdg_config_t* wdgConfig);

/*!
 * @brief Get current WDG config
 *
 * @param[in] instance: WDG instance number
 * @param[out] wdgConfig: WDG current config
 * @return None
 */
void WDG_GetCurrentConfig(uint32_t instance, wdg_config_t* wdgConfig);

/*!
 * @brief Get current counter value
 *
 * @param[in] instance: WDG instance number
 * @return currnt counter value
 */
uint16_t WDG_GetCounter(uint32_t instance);

/*!
 * @brief Set window mode
 *
 * @param[in] instance: WDG instance number
 * @param[in] enable: true - enable
 *                    fale - disable
 * @param[in] windowValue: Window value
 * @return ERR_SUCCESS:Success
 *         ERR_ERROR:Failed, invalide param or not enable update
 */
errcode_t WDG_SetWindow(uint32_t instance, bool enable, uint16_t windowValue);

/*!
 * @brief Set timeout value
 *
 * @param[in] instance: WDG instance number
 * @param[in] timeoutValue: Timeout value
 * @return ERR_SUCCESS:Success
 *         ERR_ERROR:Failed, invalide param or not enable update
 */
errcode_t WDG_SetTimeout(uint32_t instance,  uint16_t timeoutValue);

/*!
 * @brief Set interrupt enable or disable
 *
 * @param[in] instance: WDG instance number
 * @param[in] enable: true - Enable
 *                    fale - Disable
 * @return ERR_SUCCESS:Success
 *         ERR_ERROR:Failed, invalide param or not enable update
 */
errcode_t WDG_SetIntEnable(uint32_t instance,  bool enable);

/*!
 * @brief Refresh WDG counter
 *
 * @param[in] instance: WDG instance number
 * @return None
 */
void WDG_Refresh(uint32_t instance);

/*!
 * @brief Clear the WDG interrupt flag
 *
 * @param[in] instance: WDG instance number
 * @return None
 */
void WDG_ClearIntFlag(uint32_t instance);

#if defined(__cplusplus)
}
#endif

/*! @} */

#endif /* WDG_DRV_H */

/*******EOF********************************************************************/
